package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

func convertToBin(n int) string {
	// 整数转成二进制
	result := ""
	for ; n > 0; n /= 2 {
		lsb := n % 2
		result = strconv.Itoa(lsb) + result
		// strconv.Itoa 转字符串
	}
	/*todo:
	1. for的条件里不需要有括号
	2. for的条件里可以省略初始条件, 结束条件, 递增表达式
	*/
	return result
}

func forever() {
	for { // 死循环
		fmt.Println("abcdefg")
	}
}

func printFile(filename string) {
	file, err := os.Open(filename)
	if err != nil {
		panic(err)
	}
	//scanner := bufio.NewScanner(file)  // 一行一行的读
	//for scanner.Scan(){
	//	fmt.Println(scanner.Text())
	//}
	printFileContents(file)
}

func printFileContents(reader io.Reader) {
	scanner := bufio.NewScanner(reader) // 一行一行的读
	for scanner.Scan() {
		fmt.Println(scanner.Text())
	}
}

func main() {
	fmt.Println(
		convertToBin(5),  //101
		convertToBin(13), //1101
		convertToBin(872345342),
		convertToBin(0),
	)
	printFile("src/abc.txt")

	s := `"sagdfg" dsfds
		sdfdsg 123
		3534523 ds `
	printFileContents(strings.NewReader(s))
}
